ShowAssetPreviewPropertyDrawer.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace ExternPropertyAttributes.Editor
  4. {
  5. [CustomPropertyDrawer(typeof(ShowAssetPreviewAttribute))]
  6. public class ShowAssetPreviewPropertyDrawer : PropertyDrawerBase
  7. {
  8. protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label)
  9. {
  10. if (property.propertyType == SerializedPropertyType.ObjectReference)
  11. {
  12. Texture2D previewTexture = GetAssetPreview(property);
  13. if (previewTexture != null)
  14. {
  15. return GetPropertyHeight(property) + GetAssetPreviewSize(property).y;
  16. }
  17. else
  18. {
  19. return GetPropertyHeight(property);
  20. }
  21. }
  22. else
  23. {
  24. return GetPropertyHeight(property) + GetHelpBoxHeight();
  25. }
  26. }
  27. protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label)
  28. {
  29. EditorGUI.BeginProperty(rect, label, property);
  30. if (property.propertyType == SerializedPropertyType.ObjectReference)
  31. {
  32. Rect propertyRect = new Rect()
  33. {
  34. x = rect.x,
  35. y = rect.y,
  36. width = rect.width,
  37. height = EditorGUIUtility.singleLineHeight
  38. };
  39. EditorGUI.PropertyField(propertyRect, property, label);
  40. Texture2D previewTexture = GetAssetPreview(property);
  41. if (previewTexture != null)
  42. {
  43. Rect previewRect = new Rect()
  44. {
  45. x = rect.x + ExternalCustomEditorGUI.GetIndentLength(rect),
  46. y = rect.y + EditorGUIUtility.singleLineHeight,
  47. width = rect.width,
  48. height = GetAssetPreviewSize(property).y
  49. };
  50. GUI.Label(previewRect, previewTexture);
  51. }
  52. }
  53. else
  54. {
  55. string message = property.name + " doesn't have an asset preview";
  56. DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning);
  57. }
  58. EditorGUI.EndProperty();
  59. }
  60. private Texture2D GetAssetPreview(SerializedProperty property)
  61. {
  62. if (property.propertyType == SerializedPropertyType.ObjectReference)
  63. {
  64. if (property.objectReferenceValue != null)
  65. {
  66. Texture2D previewTexture = AssetPreview.GetAssetPreview(property.objectReferenceValue);
  67. return previewTexture;
  68. }
  69. return null;
  70. }
  71. return null;
  72. }
  73. private Vector2 GetAssetPreviewSize(SerializedProperty property)
  74. {
  75. Texture2D previewTexture = GetAssetPreview(property);
  76. if (previewTexture == null)
  77. {
  78. return Vector2.zero;
  79. }
  80. else
  81. {
  82. int targetWidth = ShowAssetPreviewAttribute.DefaultWidth;
  83. int targetHeight = ShowAssetPreviewAttribute.DefaultHeight;
  84. ShowAssetPreviewAttribute showAssetPreviewAttribute = PropertyUtility.GetAttribute<ShowAssetPreviewAttribute>(property);
  85. if (showAssetPreviewAttribute != null)
  86. {
  87. targetWidth = showAssetPreviewAttribute.Width;
  88. targetHeight = showAssetPreviewAttribute.Height;
  89. }
  90. int width = Mathf.Clamp(targetWidth, 0, previewTexture.width);
  91. int height = Mathf.Clamp(targetHeight, 0, previewTexture.height);
  92. return new Vector2(width, height);
  93. }
  94. }
  95. }
  96. }